//unit: mm
maxsize = 10;
cl__1 = 10;
coilcl = 5;

cH = 0.5;
cW = 0.4;
cR = 1.2;
cdx1 = 0;
cdx2 = 0;
cdy1 = 0;//-1.8;
cdy2 = 0;//-1.8;
cdz1 = 0;//2.1;
cdz2 = 0;//-2.1;
r1 = cR+cW/2;
r2 = cR-cW/2;

// ////coil+
// pcenter = newp;
// Point(pcenter+1) = {cdx1, cdy1-cH/2, cdz1};

// Point(pcenter+2) = {cdx1-r1, cdy1-cH/2, cdz1,coilcl};
// Point(pcenter+3) = {cdx1, cdy1-cH/2, cdz1+r1,coilcl};
// Point(pcenter+4) = {cdx1+r1, cdy1-cH/2, cdz1,coilcl};
// Point(pcenter+5) = {cdx1, cdy1-cH/2, cdz1-r1,coilcl};

// Point(pcenter+6) = {cdx1-r2, cdy1-cH/2, cdz1,coilcl};
// Point(pcenter+7) = {cdx1, cdy1-cH/2, cdz1+r2,coilcl};
// Point(pcenter+8) = {cdx1+r2, cdy1-cH/2, cdz1,coilcl};
// Point(pcenter+9) = {cdx1, cdy1-cH/2, cdz1-r2,coilcl};

// c5 = newc;
// Circle(c5+1) = {pcenter+2,pcenter+1,pcenter+3};
// Circle(c5+2) = {pcenter+3,pcenter+1,pcenter+4};
// Circle(c5+3) = {pcenter+4,pcenter+1,pcenter+5};
// Circle(c5+4) = {pcenter+5,pcenter+1,pcenter+2};

// Circle(c5+5) = {pcenter+6,pcenter+1,pcenter+7};
// Circle(c5+6) = {pcenter+7,pcenter+1,pcenter+8};
// Circle(c5+7) = {pcenter+8,pcenter+1,pcenter+9};
// Circle(c5+8) = {pcenter+9,pcenter+1,pcenter+6};

// l5 = newll;
// Curve Loop(l5) = {c5+1,c5+2,c5+3,c5+4};
// l6 = newll;
// Curve Loop(l6) = {c5+5,c5+6,c5+7,c5+8};
// s3 = news;
// Plane Surface(s3) = {l5, l6};

// Extrude {0, cH, 0} {Surface{s3}; }

////coil-
pcenter = newp;
Point(pcenter+1) = {cdx2, cdy2-cH/2, cdz2,coilcl};

Point(pcenter+2) = {cdx2-r1, cdy2-cH/2, cdz2,coilcl};
Point(pcenter+3) = {cdx2, cdy2-cH/2, cdz2+r1,coilcl};
Point(pcenter+4) = {cdx2+r1, cdy2-cH/2, cdz2,coilcl};
Point(pcenter+5) = {cdx2, cdy2-cH/2, cdz2-r1,coilcl};

Point(pcenter+6) = {cdx2-r2, cdy2-cH/2, cdz2,coilcl};
Point(pcenter+7) = {cdx2, cdy2-cH/2, cdz2+r2,coilcl};
Point(pcenter+8) = {cdx2+r2, cdy2-cH/2, cdz2,coilcl};
Point(pcenter+9) = {cdx2, cdy2-cH/2, cdz2-r2,coilcl};

c5 = newc;
Circle(c5+1) = {pcenter+2,pcenter+1,pcenter+3};
Circle(c5+2) = {pcenter+3,pcenter+1,pcenter+4};
Circle(c5+3) = {pcenter+4,pcenter+1,pcenter+5};
Circle(c5+4) = {pcenter+5,pcenter+1,pcenter+2};

Circle(c5+5) = {pcenter+6,pcenter+1,pcenter+7};
Circle(c5+6) = {pcenter+7,pcenter+1,pcenter+8};
Circle(c5+7) = {pcenter+8,pcenter+1,pcenter+9};
Circle(c5+8) = {pcenter+9,pcenter+1,pcenter+6};

l5 = newll;
Curve Loop(l5) = {c5+1,c5+2,c5+3,c5+4};
l6 = newll;
Curve Loop(l6) = {c5+5,c5+6,c5+7,c5+8};
s3 = news;
Plane Surface(s3) = {l5, l6};

Extrude {0, cH, 0} {Surface{s3}; }


// inf = 1.5;
// //create air box around magnets
// BoundingBox; // recompute model bounding box
// cx = (General.MinX + General.MaxX) / 2;
// cy = (General.MinY + General.MaxY) / 2;
// cz = (General.MinZ + General.MaxZ) / 2;
// lx = 2*inf + General.MaxX - General.MinX;
// ly = 2*inf + General.MaxY - General.MinZ;
// lz = 2*inf + General.MaxZ - General.MinZ;
// p1 = newp; Point (p1) = {cx-lx/2, cy-ly/2, cz-lz/2, maxsize};
// p2 = newp; Point (p2) = {cx+lx/2, cy-ly/2, cz-lz/2, maxsize};
// l1 = newl; Line(l1) = {p1, p2};
// e1[] = Extrude {0, ly, 0} { Line{l1}; };
// e2[] = Extrude {0, 0, lz} { Surface{e1[1]}; };
// Delete { Volume{e2[1]}; }//delete the box before create new one
// ss[] = {e1[1],e2[0],e2[2],e2[3],e2[4],e2[5]};
// sl1 = newsl; Surface Loop(sl1) = {ss[]};
// vv[] = {sl1};


// skin[] = CombinedBoundary{ Volume{1}; };
// sl = newsl; Surface Loop(sl) = skin[];
// vv[] += sl;
// // skin[] = CombinedBoundary{ Volume{2}; };
// // sl = newsl; Surface Loop(sl) = skin[];
// // vv[] += sl;

// // v1 = newv; 
// Volume(2) = {vv[]};

// // control mesh size
// Field[1] = Distance;
// //Field[1].NodesList = {5};
// Field[1].NNodesByEdge = 5;
// Field[1].EdgesList = {2,3,4,5,14,15,16,17};

// Field[2] = Threshold;
// Field[2].IField = 1;
// Field[2].LcMin = 0.2;
// Field[2].LcMax = 1.5;
// Field[2].DistMin = 1;
// Field[2].DistMax = 3;

// Field[3] = Distance;
// //Field[1].NodesList = {5};
// Field[3].NNodesByEdge = 20;
// Field[3].EdgesList = {6,7,8,9,18,19,20,21};

// Field[4] = Threshold;
// Field[4].IField = 3;
// Field[4].LcMin = 0.2;
// Field[4].LcMax = 1.5;
// Field[4].DistMin = 1;
// Field[4].DistMax = 3;

// // Finally, let's use the minimum of all the fields as the background mesh field
// Field[7] = Min;
// Field[7].FieldsList = {2,4};

// Background Field = 7;

// Mesh.CharacteristicLengthExtendFromBoundary = 0;
// Mesh.CharacteristicLengthFromPoints = 0;
// Mesh.CharacteristicLengthFromCurvature = 0;//+

